/*
 * GLUT Shapes Demo
 *
 * Written by Nigel Stewart November 2003
 *
 * This program is test harness for the sphere, cone
 * and torus shapes in GLUT.
 *
 * Spinning wireframe and smooth shaded shapes are
 * displayed until the ESC or q key is pressed.  The
 * number of geometry stacks and slices can be adjusted
 * using the + and - keys.
 */

#include <GL/glut.h>

#include "net.h"

#include "md3.h"

bool server;
Returninfo ret;
const int nMaxClients=2;
int nClient=0;
SOCKET csock[3];
md3 m;
md3 m1;
md3 m2;
md3 m3;

BITMAPINFO *bmp;
TGAINFO tga;

unsigned char* img;

unsigned int tex[1];

static int slices = 16;
static int stacks = 16;

/* GLUT callback Handlers */

static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	a++;

	if(server)
	{
		for(int i = 0;i<nMaxClients;i++)
		{
			NetSend(csock[i],&a, 4);
		}
	}
	else
	{
		NetRecv(ret.sock,&a,4);
	}

	drawmd3(m,tex[0]);

    glutSwapBuffers();
}


static void key(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 27 :
        case 'q':
            exit(0);
            break;

        case '+':
            slices++;
            stacks++;
            break;

        case '-':
            if (slices>3 && stacks>3)
            {
                slices--;
                stacks--;
            }
            break;
    }

    glutPostRedisplay();
}

static void idle(void)
{
    glutPostRedisplay();
}

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */

int main(int argc, char *argv[])
{
    cout<<"Loading MD3 model(s)...";
    m = loadmd3("head.md3");
	m1 = loadmd3("upper.md3");
	m2 = attach_models(m,m1);
	m3 = loadmd3("lower.md3");

	img = LoadDIBitmap("gokuHead.bmp",&bmp);
	
	cin>>server;
	cin.ignore();

	cin.get();

	ret = NetInit(server,"localhost",8888);
    glutInit(&argc, argv);
    glutInitWindowSize(320,240);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("Wolf Tech Demo");

    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutKeyboardFunc(key);
    glutIdleFunc(idle);

    glClearColor(1,1,1,1);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

	glDisable(GL_LIGHTING);
	glEnable(GL_NORMALIZE);

	glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);

	glEnable(GL_LIGHT0);

	glDisable(GL_BLEND);

	glDisable(GL_COLOR_MATERIAL);

	glEnable(GL_TEXTURE_2D);

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	glGenTextures(1,tex);

	glBindTexture(GL_TEXTURE_2D, tex[0]);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, img);

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

	if(server)
	{
		cout<<"Attempting to find connection.\n";
		NetListen(ret.sock,2);
		cout<<"Attempting to accept connection.\n";
		while(nClient<nMaxClients)
		{
			csock[nClient]=accept(ret.sock,NULL,NULL);                
			if (csock[nClient]==INVALID_SOCKET)
			{
				WSACleanup();
			}
			nClient++;
		}
		closesocket(ret.sock);
	}

    glutMainLoop();

	glDeleteTextures(1,&tex[0]);

	NetCleanup(ret.sock);

    return EXIT_SUCCESS;
}
